package cn.wolfcode.demo1.controller;


import cn.wolfcode.demo1.domain.Department;
import cn.wolfcode.demo1.qo.QueryObject;
import cn.wolfcode.demo1.service.DepartmentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("/department")
@Controller
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
//        System.out.println(1/0);
//        PageResult<Department> pageResult = departmentService.query(qo);
        PageInfo<Department> pageInfo = departmentService.query(qo);
        System.out.println(pageInfo.toString());
        model.addAttribute("pageInfo",pageInfo);
        return "department/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null){
            departmentService.delById(id);
        }
        return "redirect:/department/list";
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id!=null){
            Department department = departmentService.selOneById(id);
            model.addAttribute("department",department);
        }
        return "department/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department dept){
        if (dept.getId()!=null){
            departmentService.edit(dept);
        }else{
            departmentService.add(dept);
        }
        return "redirect:/department/list";
    }
}
